package com.zxl.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

public class ExcelExportExample {
    
    public void exportPersonList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 模拟从数据库查询数据
        List<Person> personList = queryPersonList();
        
        // 定义表头
        List<String> headers = Arrays.asList("ID", "姓名", "年龄", "生日", "是否活跃");
        // 定义字段名，对应Person类的属性名
        List<String> fields = Arrays.asList("id", "name", "age", "birthDate", "active");
        
        // 导出Excel
        ExcelExportUtil.exportExcel(response, "人员信息", "人员表", headers, fields, personList);
    }
    
    public void exportMapList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 模拟从数据库查询数据
        List<Map<String, Object>> mapList = queryMapList();
        
        // 定义表头
        List<String> headers = Arrays.asList("ID", "城市名称", "人口数量", "创建时间");
        // 定义Map的键
        List<String> fields = Arrays.asList("id", "cityName", "population", "createTime");
        
        // 导出Excel，指定日期格式和内存窗口大小
        ExcelExportUtil.exportExcel(response, "城市数据", "城市表", headers, fields, mapList, 
                                    "yyyy-MM-dd", 200);
    }
    
    // 模拟查询Person列表
    private List<Person> queryPersonList() {
        List<Person> list = new ArrayList<>();
        Person p1 = new Person(1, "张三", 25, new Date(), true);
        Person p2 = new Person(2, "李四", 30, new Date(), false);
        list.add(p1);
        list.add(p2);
        return list;
    }
    
    // 模拟查询Map列表
    private List<Map<String, Object>> queryMapList() {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("id", 1);
        map1.put("cityName", "北京");
        map1.put("population", 21540000);
        map1.put("createTime", new Date());
        
        Map<String, Object> map2 = new HashMap<>();
        map2.put("id", 2);
        map2.put("cityName", "上海");
        map2.put("population", 24240000);
        map2.put("createTime", new Date());
        
        list.add(map1);
        list.add(map2);
        return list;
    }
}

// 示例实体类
class Person {
    private Integer id;
    private String name;
    private Integer age;
    private Date birthDate;
    private Boolean active;

    public Person() {
    }

    public Person(Integer id, String name, Integer age, Date birthDate, Boolean active) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.birthDate = birthDate;
        this.active = active;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }
}